package com.ruoyi.web.handler;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.AbstractCellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;

import java.util.List;

public class HeaderStyleHandler extends AbstractCellWriteHandler {
    @Override
    public void afterCellDispose(
            WriteSheetHolder writeSheetHolder,
            WriteTableHolder writeTableHolder,
            List<WriteCellData<?>> cellDataList,
            Cell cell,
            Head head,
            Integer relativeRowIndex,
            Boolean isHead) {
        // 只处理标题行
        if (isHead) {
            // 设置单元格样式
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            CellStyle style = workbook.createCellStyle();

            // 设置背景颜色为橙色
            //style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

            // 设置居中对齐
            style.setAlignment(HorizontalAlignment.CENTER);

            // 应用样式到单元格
            cell.setCellStyle(style);

            // 设置列宽
            int columnIndex = cell.getColumnIndex();
            writeSheetHolder.getSheet().setColumnWidth(columnIndex, 20 * 256); // 设置每列宽度为30个字符
        }
    }
}
